home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue46 / packages / DinoSource.Zip / PalettePopup.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-28  |  5.7 KB  |  208 lines

  1. unit PalettePopup;
  2.  
  3. interface
  4.  
  5. procedure Register;
  6.  
  7. implementation
  8.  
  9. uses
  10.   Forms, CommonStuff, Classes, Menus, SysUtils, Dialogs;
  11.  
  12. resourcestring
  13.   SPages = '&Component palette page menu items'; //Palette menu items toggle option
  14.  
  15. const
  16.   SRegPages = 'Tab page menu items'; //Registry string
  17.   SProperties = 'Configure2'; //Palette Propertites menu item
  18.   SPaletteMenuOnClick = 'PaletteLocalPopup'; //Palette menu's OnPopup handler
  19.  
  20. type
  21.   TPalettePopup = class(TObject)
  22.   private
  23.     FProperties,
  24.     FPagesOption: TMenuItem;
  25.     FPaletteList: TList;
  26.     FOldPaletteOnPopup: TNotifyEvent;
  27.   protected
  28.     procedure AddPaletteMenuItems;
  29.     procedure DeletePaletteMenuItems;
  30.     procedure DoPages(Sender: TObject);
  31.     procedure DoClick(Sender: TObject);
  32.     procedure DoPalettePopup(Sender: TObject);
  33.     procedure SetPages(Value: Boolean);
  34.   public
  35.     constructor Create;
  36.     destructor Destroy; override;
  37.     procedure Setup;
  38.     procedure TidyUp;
  39.   end;
  40.  
  41. constructor TPalettePopup.Create;
  42. begin
  43.   inherited Create;
  44. end;
  45.  
  46. destructor TPalettePopup.Destroy;
  47. begin
  48.   TidyUp;
  49.   inherited Destroy;
  50. end;
  51.  
  52. procedure TPalettePopup.Setup;
  53. begin
  54.   //Make list for storing palette menu items
  55.   FPaletteList := TList.Create;
  56.   //Make sure there is an options menu - bear in mind
  57.   //that the other options code might not be being used
  58.   Stuff.AddOptionsItem;
  59.   //Set up component palette page options menu item
  60.   FPagesOption := NewItem(SPages, 0,
  61.     Stuff.Ini.ReadBool(SRegSection, SRegPages, False),
  62.     True, DoPages, 0, '');
  63.   //Insert the menu item
  64.   Stuff.FOptions.Add(FPagesOption);
  65.   //Set the page menu items as appropriate
  66.   SetPages(FPagesOption.Checked);
  67.  
  68.   //Chain component palette OnPopup event - this may cause problems
  69.   //if someone else chains on to it afterwards, and then we are deleted.
  70.   //The later chainer will be referring to dead code -> AV time
  71.  
  72.   //Save old OnPopup handler
  73.   FOldPaletteOnPopup := Stuff.FPalettePopup.OnPopup;
  74.   //Warn user if event was already chained
  75.   TestChainedEventHandler(TMethod(FOldPaletteOnPopup).Code,
  76.     Application.MainForm.MethodAddress(SPaletteMenuOnClick));
  77.   //Replace Delphi's event handler with our own
  78.   Stuff.FPalettePopup.OnPopup := DoPalettePopup;
  79. end;
  80.  
  81. procedure TPalettePopup.TidyUp;
  82. begin
  83.   //Save option state
  84.   Stuff.Ini.WriteBool(SRegSection, SRegPages, FPagesOption.Checked);
  85.   //Delete component palette menu items
  86.   SetPages(False);
  87.   Stuff.FOptions.Free;
  88.   Stuff.FOptions := nil;
  89.   //Unchain the chained event handler
  90.   if Assigned(Stuff.FPalettePopup) then
  91.     Stuff.FPalettePopup.OnPopup := FOldPaletteOnPopup;
  92.   //Tidy up menu list
  93.   FPaletteList.Free;
  94. end;
  95.  
  96. procedure TPalettePopup.AddPaletteMenuItems;
  97.  
  98.   procedure AddMenuItem(Menu: TMenuItem);
  99.   begin
  100.     FPaletteList.Add(Menu);
  101.     Stuff.FPalettePopup.Items.Add(Menu);
  102.   end;
  103.  
  104. var
  105.   Loop: Integer;
  106.   Item: TMenuItem;
  107. begin
  108.   //Slot an About menu item in
  109.   AddMenuItem(NewItem(SAbout, 0, False, True, Stuff.DoAbout, 0, ''));
  110.   //Add a separator
  111.   AddMenuItem(NewLine);
  112.   //Add an item for each palette page
  113.   for Loop := 0 to Stuff.FTabControl.Tabs.Count - 1 do
  114.   begin
  115.     Item := NewItem(Stuff.FTabControl.Tabs[Loop], 0, False, True, DoClick, 0, '');
  116.     Item.RadioItem := True;
  117.     //Give them some group index to make RadioItem property work
  118.     Item.GroupIndex := 57;
  119.     AddMenuItem(Item);
  120.   end;
  121.   //Add a separator
  122.   AddMenuItem(NewLine);
  123. end;
  124.  
  125. procedure TPalettePopup.DeletePaletteMenuItems;
  126. begin
  127.   //Get rid of palette tab popup menu items
  128.   while FPaletteList.Count <> 0 do
  129.   begin
  130.     TMenuItem(FPaletteList[0]).Free;
  131.     FPaletteList.Delete(0);
  132.   end;
  133. end;
  134.  
  135. procedure TPalettePopup.DoPages(Sender: TObject);
  136. begin
  137.   //Toggle existence of palette popup menu items
  138.   with Sender as TMenuItem do
  139.   begin
  140.     Checked := not Checked;
  141.     SetPages(Checked)
  142.   end
  143. end;
  144.  
  145. procedure TPalettePopup.DoClick(Sender: TObject);
  146. var
  147.   NewIndex: Integer;
  148. begin
  149.   //Work out which tab is to be selected
  150.   NewIndex := Stuff.FTabControl.Tabs.IndexOf((Sender as TMenuItem).Caption);
  151.   if NewIndex <> -1 then
  152.   begin
  153.     //Set new tab as active
  154.     Stuff.FTabControl.TabIndex := NewIndex;
  155.     //Ensure it looks like a real click by triggering OnChange event
  156.     Stuff.FTabControl.OnChange(Stuff.FTabControl)
  157.   end
  158. end;
  159.  
  160. procedure TPalettePopup.DoPalettePopup(Sender: TObject);
  161. begin
  162.   if FPagesOption.Checked then
  163.   begin
  164.     //Because the palette pages may have changed, delete the menu items...
  165.     DeletePaletteMenuItems;
  166.     //... and add new ones
  167.     AddPaletteMenuItems;
  168.     //Put the bullet mark on the currently selected page
  169.     //bearing in mind the About item and the extra new line
  170.     TMenuItem(FPaletteList.Items[Stuff.FTabControl.TabIndex + 2]).Checked := True;
  171.   end;
  172.   //Now make sure Properties lives at the end of the menu like it should
  173.   FProperties := GetComponent(Application.MainForm, SProperties, SGenericError + SProperties) as TMenuItem;
  174.   FProperties.MenuIndex := Stuff.FPalettePopup.Items.Count;
  175.   //Chain onto old OnPopup event handler
  176.   if Assigned(FOldPaletteOnPopup) then
  177.     FOldPaletteOnPopup(Stuff.FPalettePopup)
  178. end;
  179.  
  180. procedure TPalettePopup.SetPages(Value: Boolean);
  181. begin
  182.   if Value then
  183.     //This extends the local popup menu on the component palette
  184.     AddPaletteMenuItems
  185.   else
  186.     //This deletes the palette menu items
  187.     DeletePaletteMenuItems
  188. end;
  189.  
  190. var
  191.   PalettePopupObject: TPalettePopup;
  192.  
  193. procedure Register;
  194. begin
  195.   PalettePopupObject.Setup
  196. end;
  197.  
  198. initialization
  199.   try
  200.     PalettePopupObject := TPalettePopup.Create
  201.   except
  202.     on E: Exception do
  203.       ShowMessage(SSetupError + ': ' + E.Message)
  204.   end
  205. finalization
  206.   PalettePopupObject.Free
  207. end.
  208.